clear

use "C:\Users\karti\Documents\Data\Crop_production\crops_diversification_1997_2012.dta", clear


keep state year areaWheat areaRice HD

merge 1:1 state year using "C:\Users\karti\Documents\Data\regressions\paper_2\data_for_regs.dta"
drop if _m==1
drop _m

merge 1:1 state year using "C:\Users\karti\Documents\Data\regressions\paper_2\DBcontrols.dta"
drop if _m==1
drop _m

gen ln_inc = ln(real_income_per_cult_1)


keep if year<2006
gen log_sgdp = log(agrvad0405_crore)

foreach var of varlist  pop_full_new  no_agri_worker_united{
replace `var' = (`var')/10000000
}

gen rice_productivity = (rice_prod * 1000)/areaRice
gen wheat_productivity = (wheat_prod * 1000)/areaWheat


/* 
keep state year rice_productivity wheat_productivity

save "C:\Users\karti\Documents\Data\regressions\paper_2\productivity.dta", replace
*/

egen tag=tag(state)
tab marketing_reform if tag

gen OUTn = _n
gen OUTvar = ""

forvalues cat=1/3{
	gen Category`cat' = .
	gen OUT`cat' = .
}

replace Category1 = marketing_reform==0
replace Category2 = marketing_reform==1  

local n=0
foreach var of varlist rice_productivity wheat_productivity HD ln_inc rice_prod wheat_prod log_sgdp pop_full_new no_agri_worker_united Rice Wheat pop_full_new lnsdp otrev_pop ontrev_pop{
 
 local n = `n' + 1
	quietly replace OUTvar = "`var'" if OUTn == `n'
	forvalues cat = 1/2 {
		quietly reg `var' if Category`cat' == 1, cluster(state)
		local mean`cat'=_b[_cons]
		local sd`cat'=sqrt(e(rss)/e(df_r))
		local N`cat'=e(N)
		quietly replace OUT`cat' = _b[_cons] if OUTn == `n' 
	}

	local Diff=`mean1'-`mean2'
	local Welch=abs((`mean1'-`mean2')/ sqrt(`sd1'^2/`N1'+ `sd2'^2/`N2'))
	local Welch_df=(`sd1'^2/`N1'+ `sd2'^2/`N2')^2 / (`sd1'^4/`N1'^2/(`N1'-1)+ `sd2'^4/`N2'^2/(`N2'-1))
	local tprob=ttail(`Welch_df', `Welch')*2
	quietly replace OUT3 = `tprob' if OUTn == `n' 
	}
	local n = `n' + 1
replace OUTvar = "State Observations" if OUTn == `n'
foreach cat in 1 2 {
	cap drop StateTag
	egen StateTag = tag(state) if Category`cat' == 1	
	quietly count if ///
		StateTag == 1 & Category`cat' == 1
	quietly replace OUT`cat' = `r(N)' if OUTn == `n' 
}


list OUTvar OUT1 OUT2 OUT3 if !missing(OUTvar)


outsheet OUTvar OUT1 OUT2 OUT3 ///
	using "C:\Users\karti\Documents\Data\regressions\paper_2\allsumm.csv" ///
	if !missing(OUTvar), replace comma
	
	************************



	
clear

import excel "C:\Users\karti\Documents\Data\regressions\reform_status.xlsx", sheet("Sheet1") firstrow

replace date=0 if date==.

merge 1:m state using "C:\Users\karti\Documents\Data\regressions\all_agri_data_CPI_2004_05.dta"


keep if _m==3
drop _m	
gen real_income_per_cult_1 = (real_farm_1_crore*10000000)/(cultivators_united)


keep date year marketing_reform state real_income_per_cult_1
gen group =  0
replace group = 1 if marketing_reform ==0
replace group = 2 if (marketing_reform ==1 & date==2004)
replace group = 3 if (marketing_reform ==1 & date==2005)
replace group = 4 if (marketing_reform ==1 & date==2006)
replace group = 5 if (marketing_reform ==1 & date==2007)


collapse real_income_per_cult_1, by(group year)

gen ln_inc = ln(real_income_per_cult_1)

drop real_income_per_cult_1

rename ln_inc real_income_per_cult_1

reshape wide (real_income_per_cult_1), i(year) j(group)


rename real_income_per_cult_11 control
rename real_income_per_cult_12 treatment_2004
rename real_income_per_cult_13 treatment_2005
rename real_income_per_cult_14 treatment_2006
rename real_income_per_cult_15 treatment_2007

/*
foreach var of varlist control treatment_2004 treatment_2005 treatment_2006 treatment_2007{
replace `var' = `var'/1000
}


graph twoway (line control treatment_2004 treatment_2005 treatment_2006 treatment_2007 year, lwidth(medium) color(midgreen teal red ebblue dkorange)) ///
, title("Real Farm Incomes Per Cultivator (2004-05 prices)")  ///) 
xtitle("Year") ytitle("Annual Farm Incomes (in thousands)") ylabel(,labsize(vsmall)) ///
legend( cols(2) order(1 "Control States" 2 "Treatment States 2004" 3 "Treatment States 2005" 4 "Treatment States 2006" 5 "Treatment States 2007")) ///
yscale(range(10(20) 80)) ///
plotregion(fcolor(white)) graphregion(fcolor(white))
graph save trends.png, replace
*/

graph twoway (line control treatment_2004 treatment_2005 treatment_2006 treatment_2007 year, lwidth(medium) color(midgreen teal red ebblue dkorange)) ///
, title("Real Farm Incomes Per Cultivator (2004-05 prices)")  ///) 
xtitle("Year") ytitle("Natural Log of Annual Farm Incomes") ylabel(,labsize(vsmall)) ///
legend( cols(2) order(1 "Control States" 2 "Treatment States 2004" 3 "Treatment States 2005" 4 "Treatment States 2006" 5 "Treatment States 2007")) ///
plotregion(fcolor(white)) graphregion(fcolor(white))
graph save trendsln.png, replace
*********************************************************************************
clear
use "C:\Users\karti\Documents\Data\regressions\procurement_production.dta", clear

keep state year marketing_reform date rice_procurement_proportion wheat_procurement_proportion procurement_proportion

gen group =  0
replace group = 1 if marketing_reform ==0
replace group = 2 if (marketing_reform ==1 & date==2004)
replace group = 3 if (marketing_reform ==1 & date==2005)
replace group = 4 if (marketing_reform ==1 & date==2006)
replace group = 5 if (marketing_reform ==1 & date==2007)

foreach var of varlist rice_procurement_proportion wheat_procurement_proportion procurement_proportion{
replace `var'=0 if `var'==.
}


collapse rice_procurement_proportion wheat_procurement_proportion procurement_proportion, by(group year)


reshape wide (rice_procurement_proportion wheat_procurement_proportion procurement_proportion), i(year) j(group)


rename rice_procurement_proportion1 control
rename rice_procurement_proportion2 treatment_2004
rename rice_procurement_proportion3 treatment_2005
rename rice_procurement_proportion4 treatment_2006
rename rice_procurement_proportion5 treatment_2007


graph twoway (line control treatment_2004 treatment_2005 treatment_2006 treatment_2007 year, lwidth(medium) color(midgreen teal red ebblue dkorange)) ///
, title("Trends in Rice Procurement by the FCI")  ///
xtitle("Year") ytitle("Proportion of Rice Procured") ylabel(,labsize(vsmall)) ///
legend( cols(2) order(1 "Control States" 2 "Treatment States 2004" 3 "Treatment States 2005" 4 "Treatment States 2006" 5 "Treatment States 2007")) ///
yscale(range(0(.02) 0.2)) ///
plotregion(fcolor(white)) graphregion(fcolor(white))
graph save rice_fci_prop.png, replace


cap drop control treatment_2004 treatment_2005 treatment_2006 treatment_2007
 

rename wheat_procurement_proportion1 control
rename wheat_procurement_proportion2 treatment_2004
rename wheat_procurement_proportion3 treatment_2005
rename wheat_procurement_proportion4 treatment_2006
rename wheat_procurement_proportion5 treatment_2007

graph twoway (line control treatment_2004 treatment_2005 treatment_2006 treatment_2007 year, lwidth(medium) color(midgreen teal red ebblue dkorange)) ///
, title("Trends in Wheat Procurement by the FCI")  ///
xtitle("Year") ytitle("Proportion of Wheat Procured") ylabel(,labsize(vsmall)) ///
legend( cols(2) order(1 "Control States" 2 "Treatment States 2004" 3 "Treatment States 2005" 4 "Treatment States 2006" 5 "Treatment States 2007")) ///
yscale(range(0(.02) 0.2)) ///
plotregion(fcolor(white)) graphregion(fcolor(white))
graph save wheat_fci_prop.png, replace


cap drop control treatment_2004 treatment_2005 treatment_2006 treatment_2007

rename procurement_proportion1 control
rename procurement_proportion2 treatment_2004
rename procurement_proportion3 treatment_2005
rename procurement_proportion4 treatment_2006
rename procurement_proportion5 treatment_2007


graph twoway (line control treatment_2004 treatment_2005 treatment_2006 treatment_2007 year, lwidth(medium) color(midgreen teal red ebblue dkorange)) ///
, title("Trends in Wheat and Rice Procurement by the FCI")  ///
xtitle("Year") ytitle("Proportion of Wheat and Rice Procured") ylabel(,labsize(vsmall)) ///
legend( cols(2) order(1 "Control States" 2 "Treatment States 2004" 3 "Treatment States 2005" 4 "Treatment States 2006" 5 "Treatment States 2007")) ///
yscale(range(0 (.02) 0.2)) ///
plotregion(fcolor(white)) graphregion(fcolor(white))
graph save wheat_fci_prop.png, replace
***********************************************************************************************************
clear
use "C:\Users\karti\Documents\Data\regressions\procurement_production.dta", clear



foreach var of varlist Rice Wheat tot_procurement rice_prod wheat_prod {
replace `var'=0 if `var'==.
}

gen total_output = rice_prod + wheat_prod 

gen procurement_output_rice = Rice/rice_prod
gen procurement_output_wheat = Wheat/wheat_prod
gen procurement_output = tot_procurement/total_output

keep state year marketing_reform date procurement_output_rice procurement_output_wheat procurement_output rice_procurement_proportion wheat_procurement_proportion procurement_proportion

gen group =  0
replace group = 1 if marketing_reform ==0
replace group = 2 if (marketing_reform ==1 & date==2004)
replace group = 3 if (marketing_reform ==1 & date==2005)
replace group = 4 if (marketing_reform ==1 & date==2006)
replace group = 5 if (marketing_reform ==1 & date==2007)


collapse procurement_output_rice procurement_output_wheat procurement_output, by(group year)

reshape wide (procurement_output_rice procurement_output_wheat procurement_output), i(year) j(group)


rename procurement_output1 control
rename procurement_output2 treatment_2004
rename procurement_output3 treatment_2005
rename procurement_output4 treatment_2006
rename procurement_output5 treatment_2007

graph twoway (line control treatment_2004 treatment_2005 treatment_2006 treatment_2007 year, lwidth(medium) color(midgreen teal red ebblue dkorange)) ///
, title("Wheat and Rice Procured by the FCI")  ///
xtitle("Year") ytitle("Proportion of Total Output Procured") ylabel(,labsize(vsmall)) ///
legend( cols(2) order(1 "Control States" 2 "Treatment States 2004" 3 "Treatment States 2005" 4 "Treatment States 2006" 5 "Treatment States 2007")) ///
yscale(range(0.1 (.02) 0.25)) ///
plotregion(fcolor(white)) graphregion(fcolor(white))
graph save total_output_prop.png, replace

******************************************************************************************************************

** TABLE

clear
use "C:\Users\karti\Documents\Data\regressions\procurement_production.dta", clear



foreach var of varlist Rice Wheat tot_procurement rice_prod wheat_prod {
replace `var'=0 if `var'==.
}

gen total_output = rice_prod + wheat_prod 

gen procurement_output_rice = Rice/rice_prod
gen procurement_output_wheat = Wheat/wheat_prod
gen procurement_output = tot_procurement/total_output

keep state year marketing_reform date procurement_output_rice procurement_output_wheat procurement_output rice_procurement_proportion wheat_procurement_proportion procurement_proportion

gen group =  0
replace group = 1 if marketing_reform ==0
replace group = 2 if (marketing_reform ==1 & date==2004)
replace group = 3 if (marketing_reform ==1 & date==2005)
replace group = 4 if (marketing_reform ==1 & date==2006)
replace group = 5 if (marketing_reform ==1 & date==2007)


collapse procurement_output_rice procurement_output_wheat procurement_output, by(group year)

reshape wide (procurement_output_rice procurement_output_wheat procurement_output), i(group) j(year)

***********************************
**Figure 3
clear all
global filepath "C:\Users\karti\Documents\NSS_Labor_Market\Extracted_NSS"
set more off
cd "$filepath"

use "$filepath\NSS_50\02_04_final_agri_cult_43_68_data_for_reg_02_07.dta", clear

keep if age>16
keep if age<61



gen group =  0
replace group = 1 if marketing_reform ==0
replace group = 2 if (marketing_reform ==1 & date==2004)
replace group = 3 if (marketing_reform ==1 & date==2005)
replace group = 4 if (marketing_reform ==1 & date==2006)
replace group = 5 if (marketing_reform ==1 & date==2007)


foreach var of varlist agwage{

gen real_`var' = `var'/real_deflator
}



collapse (mean) real_agwage, by(group year)

gen ln_agwage = ln(real_agwage)

drop real_agwage

reshape wide (ln_agwage), i(year) j(group)


rename ln_agwage1 control
rename ln_agwage2 treatment_2004
rename ln_agwage3 treatment_2005
rename ln_agwage4 treatment_2006
rename ln_agwage5 treatment_2007

/*
foreach var of varlist control treatment_2004 treatment_2005 treatment_2006 treatment_2007{
replace `var' = `var'/1000
}


graph twoway (line control treatment_2004 treatment_2005 treatment_2006 treatment_2007 year, lwidth(medium) color(midgreen teal red ebblue dkorange)) ///
, title("Real Farm Incomes Per Cultivator (2004-05 prices)")  ///) 
xtitle("Year") ytitle("Annual Farm Incomes (in thousands)") ylabel(,labsize(vsmall)) ///
legend( cols(2) order(1 "Control States" 2 "Treatment States 2004" 3 "Treatment States 2005" 4 "Treatment States 2006" 5 "Treatment States 2007")) ///
yscale(range(10(20) 80)) ///
plotregion(fcolor(white)) graphregion(fcolor(white))
graph save trends.png, replace
*/

graph twoway (line control treatment_2004 treatment_2005 treatment_2006 treatment_2007 year, lwidth(medium) color(midgreen teal red ebblue dkorange)) ///
, title("")  ///) 
xtitle("Year") ytitle("Natural Log of Real Agricultural Wages") ylabel(,labsize(vsmall)) ///
legend( cols(2) order(1 "Control States" 2 "Treatment States 2004" 3 "Treatment States 2005" 4 "Treatment States 2006" 5 "Treatment States 2007")) ///
plotregion(fcolor(white)) graphregion(fcolor(white))
graph save trendsln.png, replace


